<?php
require_once 'AbstractStatementParser.php';
require_once (dirname(dirname(__FILE__)) .'/viewbean/Transaction.php');

	class OFXStatementParser extends AbstractStatementParser
	{
		private $OFXContent;
		private $conf;
		
		private function fixTag($tag, $acceptSpaces= false)
		{
			$tag= strtoupper($tag);
			$this->OFXContent= preg_replace('/<'.$tag.'>([\w0-9\.\-_\+\,\*#\\/!@$%&;\?'.(($acceptSpaces)? ' ': '').'])+/i', '$0</'.$tag.'>', $this->OFXContent);
		}		
		
		public function loadFromString($OFXContent)
		{	
			$originalString= $OFXContent;
			$this->OFXContent= $OFXContent;
			
			$this->OFXContent= explode('<OFX>', $this->OFXContent);
			$this->conf= $this->OFXContent[0];
			$this->OFXContent= '<OFX>'.$this->OFXContent[1];
			$this->fixTag('code');
			$this->fixTag('SEVERITY');
			$this->fixTag('INTU.BID');
			$this->fixTag('CURDEF');
			$this->fixTag('DTSERVER');
			$this->fixTag('LANGUAGE');
			$this->fixTag('TRNUID');
			$this->fixTag('BANKID');
			$this->fixTag('ACCTID');
			$this->fixTag('ACCTTYPE');
			$this->fixTag('DTSTART');
			$this->fixTag('DTEND');
			$this->fixTag('TRNTYPE');
			$this->fixTag('DTPOSTED');
			$this->fixTag('TRNAMT');
			$this->fixTag('FITID');
			$this->fixTag('CHECKNUM');
			$this->fixTag('DTASOF');
			$this->fixTag('LEDGERBAL');
			$this->fixTag('BALAMT');
			$this->fixTag('LEDGERBAL');
			$this->fixTag('MEMO', true);
			$ofx = simplexml_load_string("<?xml version='1.0'?> ".$this->OFXContent);
			
			$conf= explode('', trim($this->conf));
			$this->conf= Array();
			
			for($i=0, $j= sizeof($conf); $i<$j; $i++)
			{
				$conf[$i]= explode(':', $conf[$i], 2);
				$this->conf[$conf[$i][0]]= $conf[$i][1];
			}
			
			$this->bankAcctDetails = Array();
			$acctDet = (array)$ofx->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKACCTFROM;
				
			$acctDetailsArray = (array)$acctDet;
				
			$this->bankAcctDetails["BankName"] = $acctDetailsArray["BANKID"];
			$this->bankAcctDetails["AccountNumber"] = $acctDetailsArray["ACCTID"];
			$this->bankAcctDetails["AccountType"] = $acctDetailsArray["ACCTTYPE"];
				
				
			$this->statementBalance = (double)$ofx->BANKMSGSRSV1->STMTTRNRS->STMTRS->LEDGERBAL->BALAMT;
			$this->availableBalance = $this->statementBalance;
			
			$this->transactions = Array();
			
			foreach($ofx->BANKMSGSRSV1->STMTTRNRS->STMTRS->BANKTRANLIST->STMTTRN as $tran)
			{				
				$tranAray = (array)$tran;
				
				
				$type = $tranAray["TRNTYPE"];				
				$date = $tranAray["DTPOSTED"];
				$date = substr($date,0,4)."-".substr($date,4,2)."-".substr($date,6,2);				
				$amount = (double)$tranAray["TRNAMT"];
				$bankRef = $tranAray["MEMO"];
				$lineToHash = $date.$amount.$bankRef.$tranAray["FITID"].$this->bankAcctDetails["AccountNumber"];
				$hash = trim(md5($lineToHash));
				
				$tranObj = new Transaction(null, null, $date, $amount, $bankRef, $type, null, null, null);
				$tranObj->setHash($hash);
				$this->transactions[]= $tranObj;
			}			
			
			unset($originalString);
			unset($ofx);
		}
	}
?>
